package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.utils.MediaLoadManager;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.Content;
import flc.ast.R;
import flc.ast.adapter.SelPictureAdapter;
import flc.ast.databinding.ActivitySelPicBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.StkPermissionHelper;

public class SelPicActivity extends BaseAc<ActivitySelPicBinding> {

    private SelPictureAdapter pictureAdapter;
    private int oldPosition = 0;
    private String selPath = "";
    private List<String> listSel = new ArrayList<>();
    private boolean isAll = false;

    @Override
    protected int onCreate() {
        return R.layout.activity_sel_pic;
    }

    @Override
    protected void initView() {
        mDataBinding.ivSelPictureBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.llSelPictureSelView.setOnClickListener(this);
        mDataBinding.tvSelPictureRight.setOnClickListener(this);
        mDataBinding.rvSelPictureList.setLayoutManager(new GridLayoutManager(mContext, 3));
        pictureAdapter = new SelPictureAdapter();
        mDataBinding.rvSelPictureList.setAdapter(pictureAdapter);
        pictureAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.llSelPictureSelView://全选/全不选
                isAll = !isAll;
                mDataBinding.ivSelPictureAllSel.setImageResource(isAll ? R.drawable.quanxuan2 : R.drawable.quanxuan1);
                mDataBinding.tvSelPictureAllSel.setText(isAll?getString(R.string.cancel_all_sel):getString(R.string.all_sel));
                mDataBinding.tvSelPictureRightOff.setVisibility(isAll ? View.GONE : View.VISIBLE);
                mDataBinding.tvSelPictureRight.setVisibility(isAll ? View.VISIBLE : View.GONE);
                listSel.clear();
                for (SelectMediaEntity bean : pictureAdapter.getValidData()) {
                    bean.setChecked(isAll);
                    if (isAll) {
                        listSel.add(bean.getPath());
                    }
                }
                pictureAdapter.notifyDataSetChanged();
                break;
            case R.id.tvSelPictureRight:
                Intent intent = new Intent();
                intent.putExtra(Content.LIST_PATH, (Serializable) listSel);
                setResult(RESULT_OK, intent);
                finish();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (pictureAdapter.getItem(position).isChecked()) {
            pictureAdapter.getItem(position).setChecked(false);
            listSel.remove(pictureAdapter.getItem(position).getPath());
        } else {
            pictureAdapter.getItem(position).setChecked(true);
            listSel.add(pictureAdapter.getItem(position).getPath());
        }
        pictureAdapter.notifyItemChanged(position);
        isAllSel();
    }

    @Override
    protected void initData() {
        getPermission();
    }

    //判断是否全选
    private void isAllSel() {
        if (listSel.size() == pictureAdapter.getValidData().size()) {
            isAll = true;
            mDataBinding.ivSelPictureAllSel.setImageResource(R.drawable.quanxuan2);
            mDataBinding.tvSelPictureAllSel.setText(getString(R.string.cancel_all_sel));
        } else {
            isAll = false;
            mDataBinding.ivSelPictureAllSel.setImageResource(R.drawable.quanxuan1);
            mDataBinding.tvSelPictureAllSel.setText(getString(R.string.all_sel));
        }
        if (listSel.size() == 0) {
            mDataBinding.tvSelPictureRightOff.setVisibility(View.VISIBLE);
            mDataBinding.tvSelPictureRight.setVisibility(View.GONE);
        } else {
            mDataBinding.tvSelPictureRightOff.setVisibility(View.GONE);
            mDataBinding.tvSelPictureRight.setVisibility(View.VISIBLE);
        }
    }

    //获取权限
    private void getPermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_IMAGE_VIDEO).reqPermissionDesc(getString(R.string.get_storage_permission)).callback(new StkPermissionHelper.ACallback() {
            @Override
            public void onGranted() {
                getData();
            }

            @Override
            public void onDenied(boolean needGoSettings) {
                mDataBinding.rvSelPictureList.setVisibility(View.GONE);
                mDataBinding.tvSelPictureNoData.setVisibility(View.VISIBLE);
            }
        }).request();
    }

    //获取视频和图片数据
    private void getData() {
        RxUtil.create(new RxUtil.Callback<List<SelectMediaEntity>>() {
            @Override
            public void doBackground(ObservableEmitter<List<SelectMediaEntity>> emitter) {
                List<SelectMediaEntity> listSys = MediaLoadManager.loadAllMediaData(mContext, PhotoConfig.Mode.PHOTO);
                emitter.onNext(listSys);
            }

            @Override
            public void accept(List<SelectMediaEntity> selectMediaEntities) {
                if (selectMediaEntities.size() > 0) {
                    pictureAdapter.setList(selectMediaEntities);
                    mDataBinding.rvSelPictureList.setVisibility(View.VISIBLE);
                    mDataBinding.tvSelPictureNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvSelPictureList.setVisibility(View.GONE);
                    mDataBinding.tvSelPictureNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }

}